<template>
	<zui-box>
		<!-- 头部导航 -->

		<view class="navbar">
			<u-navbar :fixed="true" title=" " @leftClick="xz_back()" @rightClick="right_fun" :safeAreaInsetTop="true" bgColor="transparent" title-color="white">
				<view class="u-nav-slot" slot="left"><u-icon color="#ffffff" name="arrow-left" size="19"></u-icon></view>
				<view class="u-nav-slot" slot="right"><view class="right_text">我的优惠券</view></view>
				<view class="title" slot="center">优惠券市场</view>
			</u-navbar>
		</view>

	<mescroll-body :height="mescroll_height +'px'" ref="mescrollRef" @init="mescrollInit" @down="downCallback" @up="upCallback" :down="downOption"
			:up="upOption">
			
			
		<u-gap height="30rpx"></u-gap>

		<view class="list">
			<view class="item" v-for="val in list" :key="val.id">
				<zui-flat padding="0">
					<zui-panel>
						<template v-slot:left>
							<view class="coupon_box">
								<view class="price">{{ val.coupon_price }}</view>
								<u-gap height="10rpx"></u-gap>
								<view class="text">{{val.coupon_name}}</view>
							</view>
						</template>
						<template v-slot:col>
							<zui-col height="200rpx">
								<view class="name">购物券面额：{{ val.coupon_price }}</view>
								<u-gap height="10rpx"></u-gap>
								<view class="num">{{ val.create_time }}</view>
								<!-- <view class="num">开始时间：2020.11.11 00:0</view> -->
							</zui-col>
						</template>
						<template v-slot:right>
							<zui-arrBtn padding="30rpx">
								<!-- <u-button type="warning" color="#9D97A7" size="small" v-if="false" shape="circle" text="已领取"></u-button> -->
								<u-button type="warning" size="small" shape="circle" text="立即领取" @click="receive(val)"></u-button>
							</zui-arrBtn>
						</template>
					</zui-panel>
				</zui-flat>
				<u-gap height="30rpx"></u-gap>
			</view>
			
					
			
			<!-- <view class="" v-if="list.length===0"><u-empty mode="coupon" text='暂无数据'></u-empty></view> -->
		</view>
		</mescroll-body>
	</zui-box>
</template>

<script>
	import MescrollMixin from '@/uni_modules/mescroll-uni/components/mescroll-uni/mescroll-mixins.js';
	export default {
		mixins: [MescrollMixin,uni.$u.mpMixin, uni.$u.mixin],
	data() {
		return {
			list: [],
			// 下拉刷新的配置(可选, 绝大部分情况无需配置)
			downOption: {
				auto: false //是否在初始化后,自动执行downCallback; 默认true
			},
			upOption: {
				auto: false // 是否在初始化完毕之后自动执行上拉加载的回调; 默认true
			},
			mescroll_height:'',
		};
	},
	created() {
		let that = this;
		   uni.getSystemInfo({ 
			success(res) {
			console.log('一',res.windowHeight); //可使用窗口高度
			that.mescroll_height = res.windowHeight - that.$u.getPx(uni.$u.props.navbar.height) - that.$u.sys().statusBarHeight -80
			
			console.log('二',that.mescroll_height)
			}
		})
	},
	onShow() {
		this.$nextTick(()=>{
			this.mescroll.resetUpScroll();
		})
	},
	onLoad() {
		// this.getdata();
	},
	methods: {
		right_fun() {
			this.xz_go('coupon_my');
		},
		
		//下拉刷新的回调, 重置列表为第一页 (此处可删,mixins已默认)
		downCallback() { 
			setTimeout(() => {
				this.mescroll.resetUpScroll();
			}, 1000);
		},
		/*上拉加载的回调: 其中page.num:当前页 从1开始, page.size:每页数据条数,默认10 */
		 upCallback(page) {
			 
			uni.$u.http
				.get('/app/coupon/waitReceiveList', {
					params: {
						page: page.num,
					},
					custom: {
						auth: true,
						toast: false,
						catch: false,
					},
				})
				.then(res => {
					console.log('返回的数据', res);
					this.mescroll.endSuccess(res.length);
					//设置列表数据
					if (page.num == 1) this.list = []; //如果是第一页需手动制空列表
					this.list = this.list.concat(res); //追加新数据
		
				})
				.catch(res => {
					this.mescroll.endErr();
				});
		},
		
		
		// getdata() {
		// 	uni.$u.http
		// 		.get('/app/coupon/waitReceiveList', {
		// 			custom: {
		// 				auth: true,
		// 				toast: false,
		// 				catch: false
		// 			}
		// 		})
		// 		.then(res => {
		// 			this.list = res;
		// 		})
		// 		.catch(err => {});
		// },
		receive(data) {
			let that = this;
			uni.showModal({
				title: '提示',
				content: '确定要领取吗?',
				success: function(res) {
					if (res.confirm) {
						console.log('用户点击确定');
						uni.$u.http
							.post(
								'/app/coupon/receive',
								{ coupon_id: data.id },
								{
									custom: {
										auth: true,
										toast: true,
										catch: true
									}
								}
							)
							.then(res => {
								that.$u.toast('领取成功');
								that.mescroll.resetUpScroll();
							})
							.catch(err => {});
					} else if (res.cancel) {
						console.log('用户点击取消');
					}
				}
			});
		}
	}
};
</script>

<style lang="scss" scoped>
	
	/* //空数据 */
		/deep/.mescroll-empty {
			box-sizing: border-box;
			width: 100%;
			padding: 400rpx 50rpx;
			text-align: center;
		}
		
		
/* //自定义顶部栏 */
.navbar {
	font-size: 30rpx;

	/deep/.title {
		color: #fff;
	}

	/deep/.u-icon {
		padding: 10rpx;
		border-radius: 50%;
		// background:rgba(35 ,159 ,229,0.85);
	}

	.right_text {
		color: #fff;
	}
}

.name {
	font-size: 24rpx;
	color: #fff;
}
.num {
	font-size: 20rpx;
	color: #fff;
}

.coupon_box {
	border-radius: 10rpx 0 0 10rpx;
	width: 200rpx;
	height: 200rpx;
	display: flex;
	flex-flow: column;
	justify-content: space-around;
	padding: 40rpx 0;
	box-sizing: border-box;
	text-align: center;
	background: radial-gradient(circle at right top, transparent 16rpx, #eeae4f 0) right top / 100% 50% no-repeat,
		radial-gradient(circle at right bottom, transparent 16rpx, #eeae4f 0) right bottom / 100% 50% no-repeat;
	color: #fff;
	.price {
		font-size: 40rpx;
	}
	.text {
		font-size: 20rpx;
	}
}
</style>
